const baseUrl = 'http://39.96.95.38/api/'; // 接口地址

export const post = (url, data, callback) => {
	uni.showLoading({mask: true,title: '加载中'})
	uni.request({
		url: baseUrl+url,
		data: data,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/json'
		},
		method: 'POST',
		success: (response) => {
			uni.hideLoading()
			const result = response.data
			callback(result);
		},
		fail: (error) => {
			uni.hideLoading()
			if (error && error.response) {
				showError(error.response)
			}
		}
	})
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 2000
	});
}